package com.kuang.controller;

import com.kuang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class AJAXController {

    @RequestMapping("/t1")
    public String test(){
        return "hello";
    }

    @RequestMapping("/a1")
    public void a1(String name, HttpServletResponse response) throws IOException {
        System.out.println("a1:param"+name);
        if ("xiaoming".equals(name)){
            response.getWriter().print("true");
        }else {
            response.getWriter().print("false");
        }
    }

    @RequestMapping("/a2")
    public List<User> a2(){
        List<User> userList = new ArrayList<>();

        //添加数据
        userList.add(new User("小明",12,"男"));
        userList.add(new User("小白",11,"女"));
        userList.add(new User("小黒",13,"男"));
        return userList;
    }

    @RequestMapping("/a3")
    public String a3(String name,String pwd){
        String msg = null;
        if (name!=null){
            //此处的admin应是进数据库查询的用户的名字拿来判断
            if("admin".equals(name)){
                msg="ok";
            }else {
                msg="用户名错误";
            }
        }

        if (pwd!=null){
            //此处的123456应是进数据库查询的用户的名字拿来判断
            if("123456".equals(pwd)){
                msg="ok";
            }else {
                msg="密码错误";
            }
        }

        return msg;
    }
}
